package com.nCovInfo.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.nCovInfo.config.GlobalUserMap;
import com.nCovInfo.pojo.User;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/userInfo")
public class UserController {

	@ResponseBody
	@GetMapping(value = "/loadAllData")
	public JSONObject loadAllData(@RequestParam Map<String, String> map){
		Map<Integer,User> userMap = GlobalUserMap.getUserMap();
		JSONObject o = new JSONObject();
		int page = MapUtils.getIntValue(map,"page");
		int limit = MapUtils.getIntValue(map,"limit");
		JSONArray array = new JSONArray();
		userMap.values().stream().forEach(user -> array.add(user));
		o.put("code",0);
		int start = limit*(page-1);
		int end = page*limit;
		end = end>userMap.size()?userMap.size():end;
		o.put("data",array.subList(start,end));
		o.put("count",array.size());
		return o;
	}

	@ResponseBody
	@PostMapping(value = "deleteUser")
	public JSONObject deleteUser(@RequestBody Map map){
		int id = MapUtils.getIntValue(map,"id");
		Map userInfoMap = GlobalUserMap.getUserMap();
		JSONObject o = new JSONObject();
		if(userInfoMap.containsKey(id)){
			o.put("code",0);
			o.put("data",userInfoMap.remove(id));
			o.put("msg","删除成功!");
		}else{
			o.put("code",-1);
			o.put("msg","无此用户!");
		}
		return o;
	}

	@ResponseBody
	@PostMapping(value = "getUser")
	public JSONObject getUser(@RequestBody Map map){
		int id = MapUtils.getIntValue(map,"id");
		Map userInfoMap = GlobalUserMap.getUserMap();
		JSONObject o = new JSONObject();
		if(userInfoMap.containsKey(id)){
			o.put("code",0);
			o.put("data",userInfoMap.get(id));
			o.put("msg","查询成功!");
		}else{
			o.put("code",-1);
			o.put("msg","无此用户!");
		}
		return o;
	}


	@ResponseBody
	@PostMapping(value = "modifyUser")
	public JSONObject modifyUser(@RequestBody User user){
		Map userInfoMap = GlobalUserMap.getUserMap();
		userInfoMap.put(user.getId(),user);
		JSONObject o = new JSONObject();
		o.put("code",0);
		o.put("msg","修改成功!");
		return o;
	}

	@ResponseBody
	@PostMapping(value = "addUser")
	public JSONObject addUser(@RequestBody User user){
		Map userInfoMap = GlobalUserMap.getUserMap();
		if(user.getId() == null){
			user.setId(userInfoMap.size()+1);
		}
		userInfoMap.put(user.getId(),user);
		JSONObject o = new JSONObject();
		o.put("code",0);
		o.put("msg","新增成功!");
		return o;
	}

	/**
	 * 完成上传文件功能
	 * @param request
	 * @param file
	 * @return
	 */
	@RequestMapping(value = "/upload", method = {RequestMethod.GET,RequestMethod.POST},produces = "application/json;charset=UTF-8")
	@ResponseBody
	public HashMap<Object,Object> upload(HttpServletRequest request, MultipartFile file){
		if(file == null){
			HashMap<Object,Object> map1 = new HashMap<>();
			map1.put("code",0);
			map1.put("msg","上传失败");
			return map1;
		}
		//获取上传文件的文件名
		File text = FileUtil.file(file.getOriginalFilename());
		JSONArray array = new JSONArray();
		try (BufferedReader reader = new BufferedReader(new InputStreamReader(file.getInputStream()))) {
			String line;
			while((line = reader.readLine()) != null){
				List infos = Arrays.asList(line.split("\t"));
				User user = new User();
				user.setId(Convert.toInt(infos.get(0)));
				user.setUserName(Convert.toStr(infos.get(1)));
				user.setAge(Convert.toInt(infos.get(2)));
				user.setGender(Convert.toStr(infos.get(3)));
				user.setIdCardNum(Convert.toStr(infos.get(4)));
				user.setTelNum(Convert.toStr(infos.get(5)));
				boolean is = StringUtils.equals("是",Convert.toStr(infos.get(6)));
				if(is){
					user.setIsVac(1);
					user.setNumOfVac(Convert.toStr(infos.get(7)));
				}else{
					user.setIsVac(0);
				}
				GlobalUserMap.getUserMap().put(user.getId(),user);
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e){
			e.getStackTrace();
		}
		//oyp

		HashMap<Object,Object> map = new HashMap<>();
		map.put("code",0);
		map.put("msg","上传成功");

		return map;
	}
}
